﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml;
using Utility.Xml;

namespace TimeTracker.XmlProvider
{
    [Description("XML file")]
    public partial class Provider
    {
        private XDocument document = null;
        protected XDocument Document
        {
            get
            {
                if (this.document == null)
                {
                    this.document = XDocument.Load(this.LogFilePath);
                }
                return this.document;
            }
        }

        public Provider()
        {
            
        }

        IEnumerable<XProjectElement> GetProjects()
        {
            XElement test = this.Document.Root.Elements("project").FirstOrDefault();
            XProjectElement testProject = new XProjectElement(test);

            return this.Document.Root.Elements("project").Select(r => new XProjectElement(r));
        }

        XProjectElement CreateProject(XProjectElement project, string name)
        {
            XProjectElement element = new XProjectElement();
            element.BaseElement.SetAttributeValue("name", name);

            if (project != null)
            {
                project.BaseElement.Document.Root.Add(element.BaseElement);
            }
            else
            {
                this.Document.Root.Add(element.BaseElement);
            }

            return element;
        }

        XSessionElement CreateSession(XProjectElement project, DateTime start, DateTime? end)
        {
            XElement sessions = project.BaseElement.Element("sessions");
            if (sessions == null)
            {
                sessions = new XElement("sessions");
                project.BaseElement.Add(sessions);
            }

            XSessionElement element = new XSessionElement();
            element.BaseElement.SetAttributeValue("startTime", start);
            element.BaseElement.SetAttributeValue("stopTime", end);

            sessions.Add(element.BaseElement);

            return element;
        }

        XNoteElement CreateNote(XSessionElement session, DateTime time, string text)
        {
            XElement notes = session.BaseElement.Element("notes");
            if (notes == null)
            {
                notes = new XElement("notes");
                session.BaseElement.Add(notes);
            }

            XNoteElement element = new XNoteElement();
            element.BaseElement.SetAttributeValue("time", time);
            element.BaseElement.Value = text;

            notes.Add(element.BaseElement);

            return element;
        }

        void RemoveProject(IProject project)
        {
            XProjectElement element = (XProjectElement)project;

            element.BaseElement.Remove();
        }

        void RemoveSession(ISession session)
        {
            XSessionElement element = (XSessionElement)session;

            element.BaseElement.Remove();
        }

        void RemoveNote(INote note)
        {
            XNoteElement element = (XNoteElement)note;

            element.BaseElement.Remove();
        }

        System.Xml.XmlDocument GetXmlDocument()
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(this.document.CreateReader());

            return xmlDocument;
        }

        protected void Save()
        {
            this.Document.Save(this.LogFilePath);
        }
    }
}
